package com.yuan.linklist;

/**
 * @ClassName: twolinklist
 * @Description: TODO
 * @Author MrYuan
 * @Date: 2021/3/16 13:33
 * @Version 1.0
 */
public class twolinklist {
    public static void main(String[] args) {
        DoubleLinkList doubleLinkList = new DoubleLinkList();
        Node w = new Node(1, "w");
        Node l = new Node(2, "l");
        doubleLinkList.add(w);
        doubleLinkList.add(l);
        Node node = reverseList(doubleLinkList.head);
        doubleLinkList.list();
        //        System.out.println(node);
    }


    private static class DoubleLinkList {
        private Node head = new Node(0, "");

        private void add(Node node) {
            Node temp = head;
            while (temp.next != null) {
                temp = temp.next;
            }
            temp.next = node;
            node.pre = temp;
        }

        private void list() {
            Node temp = head;
            while (true) {
                if (temp == null) {
                    break;
                }
                System.out.println(temp);
                temp = temp.next;
            }
        }
    }

    private static class Node {
        private int no;
        private String name;
        Node pre;
        Node next;

        public Node(int no, String name) {
            this.no = no;
            this.name = name;
        }

        @Override
        public String toString() {
            return "no " + no + ",nodename " + name;
        }
    }

    public static Node reverseList(Node node) {
        if (node.next == null || node.next.next == null) {
            return null;
        }
        Node pre = null;
        Node next;
        while (node != null) {
            next = node.next;
            node.next = pre;
            pre = node;
            node = next;
        }
        return pre;
    }
}